//@flow

import * as Api from "../../service/api";
import { AppLoginModal } from "../../service/modal/requestParamsModal/appLoginModal";
import { UserProps,UserServiceProps } from "./userService.state";
import {UserServiceActionType} from "./userService.type";
import { ResponseBaseModal } from "../../service/modal/baseModal/responseBaseModal";

export type ActionProps = {type:UserServiceActionType} & UserServiceProps;

/**登陆 */
export async function login(data:AppLoginModal){
    return dispatch => {
        //调用登陆接口，登陆成功写进本地，登陆失败不写
        dispatch(loginDoing())
        let mock = setTimeout(()=>{
            let random:number = Math.floor(2);
            if(random ==2){
                dispatch(loginSuccess())
                return Promise.resolve({})
            }else{
                dispatch(loginFail())
                return Promise.resolve({})
            }
            
        },2000)
    }
}
/**登陆成功
 * 在reducer中更新，已登陆的所有用户
 */
export function loginSuccess(data:UserProps):ActionProps{
    return {
        type :UserServiceActionType.LOGIN_SUCCESS,
        currentUser:data,
        loginStatus:'done',
    }
}
/**登陆失败 */
export function loginFail(res:ResponseBaseModal):ActionProps{
    return {
        type:UserServiceActionType.LOGIN_FAIL,
        loginStatus:'fail'
    }
}
/**登陆中 */
export function loginDoing():ActionProps{
    return {
        type:UserServiceActionType.LOGIN_DOING,
        loginStatus:'doing'
    }
}


/**注销 */
export async function logout(){
    return dispatch => {
        //清楚登陆信息
        //注销clientID
    }
}

/**更新数据 */
export async function updateUserInfo(data:UserProps){
    
}